# -*- coding: utf-8 -*-
import os
import subprocess
import cmd

FFMPEG_PATH = os.path.dirname(os.getcwd())+'\\source\\ffmpeg.exe'
SOURCE_PATH = os.path.dirname(os.getcwd())+'\\source\\'

def __init__(self):
        super(mediaUtil, self).__init__()

'''
命令-音视频参数不变，只转换格式
inputFile:待转换的文件路径
outputFile:转换后的文件路径
bitrate:帧率
'''
def cmd_videoSimpleConvert(inputFile=None,outputFile=None,bitrate=None):
    if inputFile !=None and outputFile !=None and bitrate !=None:
        cmd = '{0} -y -i {1} -b:v {2}k {3}'.format(FFMPEG_PATH,inputFile,bitrate,outputFile)
        return cmd
    else:
        return None

'''
命令-音频参数不变，只转换格式
inputFile:待转换的文件路径
outputFile:转换后的文件路径
type:目标转换格式
'''
def cmd_audioConvert(inputFile=None,outputFile=None,type=None):
    if inputFile !=None and outputFile != None and type !=None:
        cmd = '{0} -y -i {1} -f {2} {3}'.format(FFMPEG_PATH,inputFile,type,outputFile)
        return cmd
    else:
        return None
'''
获取多媒体文件信息
'''
def cmd_mediaInfo(path):
    cmd = '{0} -i {1}'.format(FFMPEG_PATH,path)
    return cmd
    
'''
命令-剪切的同时，格式不变，音视频参数不变
inputFile:待剪切的文件路径
start_cut_time:开始剪切时间点，格式：00:00:00
end_cut_time:截止剪切时间点，格式：00:00:00
outputFile:剪切后的文件路径
'''
def cmd_videoSimpleCut(inputFile,start_cut_time,end_cut_time,bitrate,outputFile):
    if inputFile !="" and start_cut_time !="" and end_cut_time !="" and bitrate != None and outputFile !="":
        cmd='{0} -y -i {1} -ss {2} -t {3} -b:v {4}k {5}'.format(FFMPEG_PATH,inputFile,start_cut_time,end_cut_time,bitrate,outputFile)
        return cmd
    else:
        return None
    
'''音频剪切'''
def cmd_audioSimpleCut(inputFile,start_cut_time,end_cut_time,outputFile):
    if inputFile !="" and start_cut_time != "" and end_cut_time !="" and outputFile !="":
        cmd ='{0} -y -i {1} -vn -acodec copy -ss {2} -t {3} {4}'.format(FFMPEG_PATH,inputFile,start_cut_time,end_cut_time,outputFile)
        return cmd
    else:
        return None

'''获取命令执行输出的信息'''
def getCmdExecInfo(cmd):
    return subprocess.getoutput(cmd)

'''
提取视频中的音频
inputFile:视频文件地址
outputFile:音频文件地址
'''
def cmd_getVideoVn(inputFile,outputFile):
    if inputFile !="" and outputFile !="":
        cmd ='{0} -y -i {1} -f mp3 -vn {2}.mp3'.format(FFMPEG_PATH,inputFile,outputFile)
        return cmd
    else:
        return None
'''
提取视频文件中的视频
inputFile:视频文件地址
outputFile:提取的视频文件地址
bitrate:帧率
'''
def cmd_getVideoAn(inputFile=None,outputFile=None,bitrate=None):
    if inputFile !=None and outputFile !=None and bitrate !=None:
        cmd = '{0} -y -i {1} -b:v {2}k -an {3}'.format(FFMPEG_PATH,inputFile,bitrate,outputFile)
        return cmd
    else:
        return None

'''视频画面旋转命令'''    
def cmd_getVideoTSP(inputFile=None,outputFile=None,rotate=None,bitrate=None):
    if inputFile !="" and outputFile !="" and rotate != None and bitrate != None:
        cmd ='{0} -y -i {1} -vf vflip,hflip -b:v {2}k -metadata:s:v:0 rotate={3} {4}'.format(FFMPEG_PATH,inputFile,bitrate,rotate,outputFile)
        return cmd
    else:
        return None

'''获取秒数'''
def getSecond(time):
    ts = time.split(':')
    return float(ts[0])*3600+float(ts[1])*60+float(ts[2])

'''
根据秒数获取时分秒时间字符串
格式：00:00:00
'''
def getHMS(second):
    duration='{0}:{1}:{2}'
    if second>0:
        hour = int(second/3600)
        if hour>0:
            mm = int(second%3600/60)
            ss = int(second%3600%60)
            duration=duration.format('{:0>2d}'.format(hour),'{:0>2d}'.format(mm),'{:0>2d}'.format(ss))
        else:
            mm = int(second/60)
            ss = int(second%60)
            duration=duration.format('{:0>2d}'.format(0),'{:0>2d}'.format(mm),'{:0>2d}'.format(ss))
    else:
        duration='00:00:00'
    return duration
    